<template>
  <common-list>
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        @submit.native.prevent
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        @enter="handleQuery"
        label-width="65px"
      >
        <el-form-item label="报告名称" prop="logName">
          <common-input
            v-model="queryParams.logName"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="负责人" prop="principal">
          <common-input
            v-model="queryParams.principal"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <!-- <common-dept
          ref="commonDept"
          :currentParent="currentParent"
          @getDeptId="getDeptId"
          @getDeptList="getDeptList"
          @getLoading="getLoading"
        ></common-dept> -->

        <el-form-item label="创建时间">
          <el-date-picker
            v-model="date"
            type="datetimerange"
            @change="dateChange"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            :default-time="['00:00:00', '23:59:59']"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
    </common-search>

    <common-table
      v-loading="loading"
      slot="content"
      :data="reportList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="报告名称"
        align="left"
        prop="logName"
        show-overflow-tooltip
        width="500"
      />
      <el-table-column
        label="负责人"
        align="left"
        prop="principal"
        show-overflow-tooltip
      />

      <!-- <el-table-column
        label="组织"
        align="left"
        prop="deptName"
        show-overflow-tooltip
      /> -->

      <el-table-column
        label="开始日期"
        align="left"
        prop="beginTime"
        show-overflow-tooltip
      />

      <el-table-column
        label="结束日期"
        align="left"
        prop="endTime"
        show-overflow-tooltip
      />

      <el-table-column
        label="创建时间"
        align="left"
        prop="createTime"
        width="180"
      />

      <el-table-column label="操作" align="left" width="60px">
        <template slot-scope="scope">
          <el-button type="text" @click="handleDetail(scope.row)"
            >详情
          </el-button>
        </template>
      </el-table-column>
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <detail-tab
      v-if="isShowDetail"
      :id="id"
      @back="isShowDetail = false"
    ></detail-tab>
  </common-list>
</template>

<script>
import { fetchList } from "@/api/datascan/dataAssetsReport/index";
import DetailTab from "./components/detailTab.vue";
import BaseList from "@/mixins/BaseList";
import { getSingleArray, getFullDept } from "@/utils/util";
export default {
  mixins: [BaseList],
  components: {
    DetailTab,
  },
  data() {
    return {
      ids: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        logName: null,
        principal: null,
        deptIds: null,
      },
      total: 0,
      reportList: [],
      loading: false,
      isShowDetail: false,
      id: "",
      currentParent: 4,
      singleDeptList: [],
      deptListParams: [],
      date: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    dateChange() {
      let endTime = null,
        beginTime = null;
      if (this.date && this.date.length > 0) {
        beginTime = this.date[0];
        endTime = this.date[1];
      }
      this.queryParams.beginTime = beginTime;
      this.queryParams.endTime = endTime;
    },
    getList() {
      this.loading = true;
      fetchList(this.queryParams)
        .then((res) => {
          if (res && res.code === 200) {
            this.reportList = (res.data && res.data.dataList) || [];

            // this.reportList.forEach((item) => {
            //   item.deptName = this.getDeptName(item.deptId || "");
            // });
            this.total = res.data.totalCount;
          } else {
            this.reportList = [];
            this.total = 0;
            this.handleListTips(res);
          }
        })
        .catch(() => {
          this.reportList = [];
          this.total = 0;
        })
        .finally(() => {
          this.loading = false;
        });
    },

    getLoading() {
      this.loading = true;
    },

    getDeptList(list) {
      this.deptListParams = list;
      this.singleDeptList = getSingleArray(list);
      this.getList();
    },
    getDeptId(val) {
      this.queryParams.deptIds = val;
    },

    // getDeptName(val) {
    //   let dept = this.singleDeptList.find((item) => item.id === val.toString());
    //   if (dept) return getFullDept(dept.fulllabel);
    // },

    handleDetail(row) {
      this.id = row.id.toString();
      this.isShowDetail = true;
    },

    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams = this.$options.data().queryParams;
      // this.$refs.commonDept.clearDept();
      this.date = [];
    },
  },
};
</script>

<style lang="scss"></style>
